﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Quartz;
using Quartz.Impl;
using Swashbuckle.AspNetCore.Swagger;
using System;
using System.IO;
using System.Reflection;

namespace H2O.Api
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            // 日志服务
            services.AddLogging();

            // 调度服务
            services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();

            // Swagger
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc(name: "v1", new Info
                {
                    Contact = new Contact
                    {
                        Email = "i@taadis.com",
                        Name = "taadis",
                        Url = "https://gitee.com/taadis/H2O"
                    },
                    Description = "接口文档描述, 基于 swagger (丝袜哥)",
                    License = null,
                    TermsOfService = "",
                    Title = "H2O API Doc",
                    Version = "v1"
                });

                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                options.IncludeXmlComments(xmlPath);
            });

            // 跨域支持
            services.AddCors(options =>
            {
                options.AddPolicy(name: "*", configurePolicy: builder =>
                {
                    builder.AllowAnyOrigin();
                    builder.AllowAnyMethod();
                    builder.AllowAnyHeader();
                });
            });

            // MVC
            services.AddMvc();
            services.AddRouting(options =>
            {
                options.LowercaseUrls = true;
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();
            app.UseFileServer();
            app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "H2O API");
                options.RoutePrefix = null;
            });
            app.UseCors(policyName: "*");
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
